package model

import (
	"fmt"
	"gin-rkboot-sw/internal/global"
	rkentry "github.com/rookie-ninja/rk-entry/v2/entry"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

func InitDB() error {
	databaseConfig := rkentry.GlobalAppCtx.GetConfigEntry("my-config").GetStringMap("database")
	dsn := fmt.Sprintf("%v:%v@tcp(%v:%v)/%v?charset=utf8mb4&parseTime=True&loc=Local",
		databaseConfig["username"],
		databaseConfig["password"],
		databaseConfig["host"],
		databaseConfig["port"],
		databaseConfig["db"],
	)
	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
	global.DB = db
	return err
}
